package com.li.youhappygo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.li.youhappygo.mapper.UserMapper;
import com.li.youhappygo.model.dto.UserDto;
import com.li.youhappygo.model.dto.UserQueryParamDto;
import com.li.youhappygo.model.entity.user.Users;
import com.li.youhappygo.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public PageInfo<Users> getUserList(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Users> users = userMapper.selectList(null);
        PageInfo<Users> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public Users getUserInfoById(Integer id) {
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        Users users = userMapper.selectOne(wrapper);
        return  users;
    }

    @Override
    public void updateUserInfo(UserDto userDto) {
        Users users = new Users();
        BeanUtils.copyProperties(userDto,users);
        userMapper.updateById(users);
    }

    @Override
    public void deleteUser(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    public void addUser(Users user) {

        Users new_user = new Users();
        BeanUtils.copyProperties(user,new_user);
        userMapper.insert(new_user);
        System.out.println(new_user.getId());
    }

    @Override
    public PageInfo<Users> getByUserListByQueryParam(UserQueryParamDto userQueryParamDto, Integer pageSize, Integer pageNum) {
        LambdaQueryWrapper<Users> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(userQueryParamDto.getSex()!=null,Users::getSex,userQueryParamDto.getSex())
                .eq(userQueryParamDto.getStatus()!=null,Users::getStatus,userQueryParamDto.getStatus())
                .like(userQueryParamDto.getUsername()!=null,Users::getUsername,userQueryParamDto.getUsername());
        PageHelper.startPage(pageNum,pageSize);
        List<Users> usersList = userMapper.selectList(lambdaQueryWrapper);
        PageInfo<Users> pageInfo = new PageInfo<>(usersList);
        return pageInfo;
    }
}
